﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using SmartSpace.UI.Silverlight;
using SmartSpace.UI.Silverlight.Theme;
using System.Windows.Media;

namespace SmartSpace.UI.Silverlight.Themes.ScienceFiction
{
    public partial class HeaderPanel : SlView, IHeaderPanel
    {
        #region Variables

        private LoginUser m_user;

        private SlApplication m_currentApplication;

        public event NavigatingEventHandler Navigating;

        #endregion

        #region Constructors

        public HeaderPanel()
        {
            InitializeComponent();
        }

        #endregion

        #region IHeaderPanel Members

        public void Initiate(IEnumerable<SlShortCut> shortCuts, List<SlApplication> applications, SlApplication currentApplication, LoginUser user)
        {
            m_user = user;

            if (!string.IsNullOrEmpty(currentApplication.IconID))
            {
                m_imgLogo.Source = new BitmapImage(new Uri(currentApplication.IconPath, UriKind.Relative));
            }

            m_textAppName.Text = currentApplication.Name;
            m_textUserName.Text = "欢迎您:" + user.Name + "!";

            m_currentApplication = currentApplication;
            // 添加切换系统的菜单
            m_comboBoxApps.Items.Clear();
            foreach (SlApplication app in applications)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = app.ShortName;
                item.Tag = app;

                m_comboBoxApps.Items.Add(item);
                if (app.ID == currentApplication.ID)
                {
                    m_comboBoxApps.SelectedItem = item;
                }
            }

            bool isFirstShortCut = true;
            foreach (SlShortCut shortCut in shortCuts)
            {
                if (isFirstShortCut)
                {
                    StackPanel stackPanel = new StackPanel();
                    stackPanel.Orientation = Orientation.Horizontal;
                    stackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                    if (!string.IsNullOrEmpty(shortCut.IconPath))
                    {
                        Image image = new Image();
                        image.Width = 16;
                        image.Height = 16;
                        image.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                        image.Source = new BitmapImage(new Uri(shortCut.IconPath, UriKind.Relative));
                        stackPanel.Children.Add(image);
                    }

                    TextBlock text = new TextBlock();
                    text.Text = shortCut.Name;
                    text.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 254));
                    text.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    text.Margin = new Thickness(3, 0, 0, 0);
                    stackPanel.Children.Add(text);


                    HyperlinkButton button = new HyperlinkButton();
                    button.Tag = shortCut;
                    button.Content = stackPanel;
                    button.IsTabStop = false;
                    button.Margin = new Thickness(5, 0, 0, 0);
                    button.Click += new RoutedEventHandler(button_Click);

                    m_stackPanelShortCuts.Children.Add(button);

                    isFirstShortCut = false;
                }
            }
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton button = sender as HyperlinkButton;
            SlShortCut shortCut = button.Tag as SlShortCut;
            if (shortCut != null)
            {
                if (Navigating != null)
                {
                    (ThemeManager.CurrentTheme.GetMainPanel().GetNavigatePanel() as NavigatePanel).LostNavigationFocus();
                    Navigating(this, new NavigatingEventArgs(shortCut.Name, shortCut.View));
                }
            }
        }

        public event LogoffEventHandler Logoff;

        public event EventHandler EnterManagement;

        public event ChangeAppEventHandler ChangeApp;

        public event Action<bool> FullScreenEventHandler;

        #endregion

        #region Button Event Handlers

        /// <summary>
        /// 注销按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnButtonLogOffClick(object sender, RoutedEventArgs e)
        {
            if (Logoff != null)
            {
                Logoff(this, new LogoffEventArgs(m_user));
            }
        }

        /// <summary>
        /// 进入管理后台操作
        /// 暂时不使用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnButtonManagerClick(object sender, RoutedEventArgs e)
        {
            if (EnterManagement != null)
            {
                EnterManagement(this, new EventArgs());
            }
        }

        /// <summary>
        /// 全屏幕按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnButtonFullScreenClick(object sender, RoutedEventArgs e)
        {
            HyperlinkButton buttonFull = sender as HyperlinkButton;
            bool isFullSrceen = false;
            if (string.Equals(buttonFull.Content.ToString(), "全屏显示"))
            {
                buttonFull.Content = "退出全屏";
                m_imageFullScreen.Source = new BitmapImage(new Uri("/SmartSpace.UI.Silverlight.Themes.ScienceFiction;component/Resources/smallscreen.PNG", UriKind.RelativeOrAbsolute));
                isFullSrceen = true;
            }
            else if (string.Equals(buttonFull.Content.ToString(), "退出全屏"))
            {
                buttonFull.Content = "全屏显示";
                m_imageFullScreen.Source = new BitmapImage(new Uri("/SmartSpace.UI.Silverlight.Themes.ScienceFiction;component/Resources/fullscreen.PNG", UriKind.RelativeOrAbsolute));
                isFullSrceen = false;
            }

            if (FullScreenEventHandler != null)
            {
                FullScreenEventHandler(isFullSrceen);
            }
        }

        /// <summary>
        /// 单击注销图片进行注销操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnImageLogoutMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnButtonLogOffClick(null, null);
        }

        /// <summary>
        /// 单击全屏显示按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnImageFullScreenMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnButtonFullScreenClick(m_buttonFullScreen, null);
        }

        #endregion

        #region ComboBox Event Handlers

        private void OnComboBoxAppsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem item = m_comboBoxApps.SelectedItem as ComboBoxItem;
            SlApplication app = item.Tag as SlApplication;
            if (app != m_currentApplication)
            {
                if (ChangeApp != null)
                {
                    ChangeApp(this, new ChangeAppEventArgs(app));
                }
            }
        } 

        private void OnComboBoxAppsDropDownOpened(object sender, EventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            comboBox.Width = comboBox.ActualWidth;
        }

        private void OnComboBoxAppsDropDownClosed(object sender, EventArgs e)
        {
        }

        #endregion
    }
}
